package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"table-compare/db"
	"table-compare/service"
)

func main() {
	db.SetupDb()
	service.Setup()
	router := gin.Default()
	router.GET("/api/system/list", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"code": 200,
			"data": service.SYSTEM_SERVICE.List(),
		})
	})
	router.GET("/api/table/list", func(c *gin.Context) {
		param := c.Query("systemId")
		if param == "" {
			c.JSON(400, gin.H{
				"code": 400,
				"msg":  "参数错误",
			})
			return
		}
		p, _ := strconv.Atoi(param)
		c.JSON(200, gin.H{
			"code": 200,
			"data": service.TABLE_SERVICE.GetTablesBySystemId(uint(p)),
		})
	})
	router.GET("/api/column/list", func(c *gin.Context) {
		param := c.Query("tableId")
		if param == "" {
			c.JSON(400, gin.H{
				"code": 400,
				"msg":  "参数错误",
			})
			return
		}
		p, _ := strconv.Atoi(param)
		c.JSON(200, gin.H{
			"code": 200,
			"data": service.COLUMN_SERVICE.GetColumnsByTableId(uint(p)),
		})
	})

	router.GET("/api/new/table/list", func(c *gin.Context) {

		c.JSON(200, gin.H{
			"code": 200,
			"data": service.NEW_TABLE_SERVICE.GetTables(),
		})
	})
	router.GET("/api/new/column/list", func(c *gin.Context) {
		param := c.Query("tableId")
		if param == "" {
			c.JSON(400, gin.H{
				"code": 400,
				"msg":  "参数错误",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 200,
			"data": service.NEW_COLUMN_SERVICE.GetColumns(param),
		})
	})
	// 静态文件目录，部署时候使用
	router.StaticFS("/web", http.Dir("/app/html"))
	router.Run(":8087")
}
